<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML" 4.01"//EN" "http://www.w3.org/TR/html4/strict.dtd">
<!-- Created: September 13, 2011-->
<html>
<head>
<title>Radiological Equipment Database</title>
<meta name="author" content="Eugene Mah" />
<link rev="made" href="mailto:eugenem@ix.netcom.com" />
<link rev="home" href="http://blog.imabug.net/" />
<meta name="description" content="" />
<meta name="Keywords" content="" />
<meta name="Reply-to" content="eugenem@ix.netcom.com" />
<?PHP
require('functions.inc');
require('dbc.inc');

// Get the current date
date_default_timezone_set('America/New_York');
$today = date("Ymd");
$year = date("Y");
$month = date("m");
$day = date("d");
?>
<style type="text/css">
.red {background-color: #FF3333;}
.orange {background-color: #FF8000;}
.yellow {background-color: #FFFF33;}
.green {background-color: #33FF33;}
.blue {background-color: #80FFFF;}
td {text-align: center;}
</style>
</head>

<body>
<h1>Radiological Equipment Database</h1>
<h2>Equipment Testing Status Dashboard</h2>
<h3>Table legend</h3>
<table>
  <tr>
    <td class="green">Current</td>
    <td class="yellow">Due within 30 days</td>
    <td class="orange">Overdue &lt; 13 months</td>
    <td class="red">Overdue &gt; 13 months</td>
    <td class="blue">Scheduled, not tested yet</td>
  </tr>
</table>
<?PHP
// Get the list of modalities
$modResult = mysql_query("select * from Modality order by Modality");

// Cycle through each modality
while ($modality = mysql_fetch_array($modResult)) {
  // Fetch the list of equipment
  $query = "SELECT MachineID,Modality,Manufacturer,Description,Location
            FROM Modality,Manufacturer,Location,Machines
            WHERE Machines.ModalityID=".$modality['ModalityID']."
            AND Machines.Status=\"Active\"
            AND Machines.SurveyFreq<>\"NA\"
            AND Modality.ModalityID=Machines.ModalityID
            AND Manufacturer.ManufacturerID=Machines.ManufacturerID
            AND Location.LocationID=Machines.LocationID
            ORDER BY Description";
  $machResult = mysql_query($query)
    or die ("Query failed: ".$query."<br />\n");
  // Find out how many results were returned
  $machCount = mysql_num_rows($machResult);

  // Want to create a 5xn table to display the results. Figure out n
  $n = (int)ceil($machCount/5);
  // Create the table for this modality
  echo "<h3>".$modality['Modality']."</h3>\n";
  echo "<table border='1'>\n";
  for ($i=0;$i<$n;$i++) {
    echo "  <tr>\n";
    for ($j=0;$j<5;$j++) {
      if ($machine = mysql_fetch_array($machResult)) {
        // Get the most recent test date (annual survey or acceptance test)and calculate the number of days
        // since then for this machine
        $query = "SELECT SurveyID,TestDate,TO_DAYS(curdate()) - TO_DAYS(TestDate) as days FROM TestDates WHERE MachineID=".$machine['MachineID'] . " and (TypeID=1 or TypeID=2) ORDER BY TestDate DESC LIMIT 1";
        $machTestDate = mysql_fetch_array(mysql_query($query));
        $days = $machTestDate['days'];
        // Set the table cell ID based on the number of days since the last survey
        if ($days < 335) {
          // Survey is current. < 11 mo since last survey
          if ($machTestDate['TestDate'] > date('Y-m-d')) {
            // Show in blue if the survey date is after today (scheduled, but not tested yet)
            echo "    <td class='blue'>\n";
          }
          else {
            echo "    <td class='green'>\n";
          }
        }
        elseif (($days >= 335) && ($days < 365)) {
          // Survey is due within 30 days
          echo "    <td class='yellow'>\n";
        }
        elseif (($days >= 365) && ($days < 395)) {
          // Survey is overdue but less than 30 days overdue
          echo "    <td class='orange'>\n";
        }
        elseif ($days > 395) {
          // Suvey is more than 13 months overdue
          echo "    <td class='red'>\n";
        }
        else {
          echo "    <td>\n";
        }
        // TODO Link description to report page for that machine
        echo "<a href=\"RepMachine.php?MachID=" . $machine['MachineID'] . "\">". $machine['Description']."</a><br />\n";
        echo $machine['Location']."<br />\n";
        // TODO Link test date to survey recommendations page for that survey
        echo "<a href=\"SurveyResRec.php?SurveyID=" . $machTestDate['SurveyID'] . "\">" . $machTestDate['TestDate'] . "</a><br />\n";
//        echo $machine['MachineID']."<br />\n";
//        echo $days."<br />\n";
        echo "    </td>\n";
      }
      else break;
    }
    echo "  </tr>\n";
  }
  echo "</table>\n";
}

?>
<h3>Table legend</h3>
<table>
  <tr>
    <td class="green">Current</td>
    <td class="yellow">Due within 30 days</td>
    <td class="orange">Overdue &lt; 13 months</td>
    <td class="red">Overdue &gt; 13 months</td>
    <td class="blue">Scheduled, not tested yet</td>
  </tr>
</table>
<a href="http://radinfo.musc.edu/ComplianceReports/" target="_top">Main</a>
</body>
